using System;
using System.Data;
using System.Data.SqlClient;
using Entity;

namespace SalesVMT
{
    public class IncomeDB
    {
        public static DataSet GetOne(int _Income_ID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Income_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Income_ID", _Income_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public static DataSet GetList(out int ItemCount, string keyWord, int currentPage, int pageSize)
        {

            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Income_GetList", dbConn);

            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
        public static bool Delete(int _Income_ID)
        {
            bool retVal = false;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Income_Delete", dbConn);
            dbCmd.Parameters.AddWithValue("@Income_ID", _Income_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Insert(IncomeInfo _IncomeInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Income_Insert", dbConn);
            dbCmd.Parameters.AddWithValue("@Income_Code", _IncomeInfo.Income_Code);
            dbCmd.Parameters.AddWithValue("@Income_Date", _IncomeInfo.Income_Date);
            dbCmd.Parameters.AddWithValue("@Income_CustomerID", _IncomeInfo.Income_CustomerID);
            dbCmd.Parameters.AddWithValue("@Income_Receive", _IncomeInfo.Income_Receive);
            dbCmd.Parameters.AddWithValue("@Income_Reason", _IncomeInfo.Income_Reason);
            dbCmd.Parameters.AddWithValue("@Income_Total", _IncomeInfo.Income_Total);
            dbCmd.Parameters.AddWithValue("@Income_UsersID", _IncomeInfo.Income_UsersID);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public static IncomeInfo GetInfo(int _Income_ID)
        {
            IncomeInfo retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Income_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Income_ID", _Income_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new IncomeInfo();
                    retVal.Income_ID = Convert.ToInt32(dr["Income_ID"]);
                    retVal.Income_Code = Convert.ToString(dr["Income_Code"]);
                    retVal.Income_Date = Convert.ToDateTime(dr["Income_Date"]);
                    retVal.Income_CustomerID = Convert.ToInt32(dr["Income_CustomerID"]);
                    retVal.Income_Receive = Convert.ToString(dr["Income_Receive"]);
                    retVal.Income_Reason = Convert.ToString(dr["Income_Reason"]);
                    retVal.Income_Total = Convert.ToDecimal(dr["Income_Total"]);
                    retVal.Income_UsersID = Convert.ToInt32(dr["Income_UsersID"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public static string GetMaxCode()
        {
            string retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Income_GetMaxCode", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToString(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        
        
    }
}
